//
//  YCEntryFeePayViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/3.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  报名费支付

#import "YCEntryFeePayViewController.h"
#import "YCEntryPayHeadInfoView.h"
#import "YCApplyPayCell.h"
#import "YCApplyCostPayModel.h"
#import "YCFillInfomationModel.h"

@interface YCEntryFeePayViewController ()<UITableViewDelegate,UITableViewDataSource>

/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/** containerView */
@property (nonatomic, strong) UIView *containerView;

/** 表  */
@property (nonatomic, strong) UITableView *tableView;
/** 头  */
@property (nonatomic, strong) YCEntryPayHeadInfoView *headView;
/** 底部视图  */
@property (nonatomic, strong) UIView *footerView;
/** 确认支付  */
@property (nonatomic, strong) UIButton *confirmPayButton;// 确认支付按钮

/** tabelView数据  */
@property (nonatomic, strong) NSArray *datasArray;

/** 填写资料的模型  */
@property (nonatomic, strong) YCFillInfomationModel *fillInfoModel;
@end

@implementation YCEntryFeePayViewController

- (NSArray *)datasArray
{
    if (!_datasArray) {
        YCApplyCostPayModel *model = [[YCApplyCostPayModel alloc] init];
        model.select = NO;
        model.payMethod = @"全额支付";
        model.costValue = 8800;
        
        YCApplyCostPayModel *model1 = [[YCApplyCostPayModel alloc] init];
        model1.select = NO;
        model1.payMethod = @"预付金支付";
        model1.costValue = 600;
        
        _datasArray = @[model,model1];
    }
    return _datasArray;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.scrollEnabled = NO;//不允许滚动
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (YCEntryPayHeadInfoView *)headView
{
    if (!_headView) {
        _headView = [[YCEntryPayHeadInfoView alloc] init];
    }
    return _headView;
}


- (UIButton *)confirmPayButton
{
    if (!_confirmPayButton) {
        _confirmPayButton = [[UIButton alloc] init];
        _confirmPayButton.backgroundColor = [UIColor yc_colorWithUInt:0x5dc811];
        _confirmPayButton.layer.cornerRadius = 35 / 2;
        _confirmPayButton.layer.masksToBounds = YES;
        [_confirmPayButton setTitle:@"报名费支付" forState:UIControlStateNormal];
        _confirmPayButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_confirmPayButton addTarget:self action:@selector(didTouchUpInsideConfirmPayButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmPayButton;
   
}
- (void)makeView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.alwaysBounceVertical = YES;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor whiteColor];
    self.containerView = containerView;
    [self.scrollView addSubview:containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.containerView addSubview:self.headView];
    [self.containerView addSubview:self.tableView];
    [self.containerView addSubview:self.confirmPayButton];
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.containerView);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom);
        make.left.right.equalTo(self.containerView);
        make.height.equalTo(@96);//tableView的高度
    }];
    
    [self.confirmPayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(heightFromIphone6Height(95));
        make.centerX.equalTo(self.containerView);
        make.left.equalTo(self.containerView).offset(50);
        make.right.equalTo(self.containerView).offset(-50);
        make.height.equalTo(@35);
    }];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.confirmPayButton.mas_bottom).offset(heightFromIphone6Height(54));
    }];
    
    [self.scrollView layoutIfNeeded];
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.containerView.frame));
    
}

#pragma mark - 生命周期

- (id)initWithFillInfoModel:(YCFillInfomationModel *)model
{
    if (self = [super init]) {
        _fillInfoModel = model;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self yc_addLeftBarButtonItemWithTitle:@"关闭" target:self action:@selector(close)];
    [self makeView];
    self.headView.model = self.fillInfoModel;
    self.navigationItem.title = @"报名费支付";

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCApplyPayCell *cell = [YCApplyPayCell tableViewCellWithTableView:tableView withIndexPath:indexPath];
    cell.model = self.datasArray[indexPath.row];
    return cell;
}

#pragma mark - UITableView delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCApplyCostPayModel *model = self.datasArray[indexPath.row];
    model.select = !model.isSelect;
    for (YCApplyCostPayModel *otherModel in self.datasArray) {
        if (otherModel != model) {
            otherModel.select = NO;
        }
    }
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}
#pragma mark - 点击事件监听

- (void)close
{
    YCLog(@"关闭");
    NSArray * ctrlArray = self.navigationController.viewControllers;
    for (UIViewController *vc in ctrlArray) {
        YCLog(@"%@",vc);
    }
    
    //    [self.navigationController popToViewController:[ctrlArray objectAtIndex:1] animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 确定支付
- (void)didTouchUpInsideConfirmPayButton:(UIButton *)payButton
{
    YCLogFunc
}



@end
